<template>
  <div id="main" style="width: 600px;height:400px;"></div>
</template>

<script>
// 引入 ECharts 主模块
import echarts from 'echarts'
import _ from 'lodash'

export default {
  data() {
    return {
      data: {},
      options: {
        title: {
          text: '用户来源'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            boundaryGap: false
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ]
      }
    }
  },
  created() {
    this.getReportData()
  },
  mounted() {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'))
    // 合并数据
    const res = _.merge(this.data, this.options)
    // 绘制图表
    myChart.setOption(res)
    console.log(res)
  },
  methods: {
    async getReportData() {
      const { data: res } = await this.$http.get('reports/type/1')
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.data = res.data
    }
  }
}
</script>

<style lang="less" scoped></style>
